//
//  UIView+Friend.m
//  AiCheSong
//
//  Created by 李贤立 on 2017/12/14.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "UIView+Friend.h"

@implementation UIView (Friend)
/**
 *  模仿系统弹框的温和动画
 */
- (void)animationAlert {
    self.alpha = 0;
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.55 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
/**
 *  模仿系统弹框的抖动动画
 */
- (void)animationShake {
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.3;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.layer addAnimation:popAnimation forKey:nil];
}
/**
 *  没有数据的时候显示的图片
 */
- (void)noData {
    [self noDataWithSink:0 click:nil];
}
- (void)noDataWithClick:(void (^)(void))click {
    BaseInfo.clickNoDataViewBlock = click;
    [self noDataWithMessage:[@"点击重新加载" S] click:click];
}
/**
 *  没有数据的时候显示的图片
 *
 *  @pram message 显示的文字
 */
- (void)noDataWithMessage:(NSString *)message click:(void (^)(void))click {
    BaseInfo.clickNoDataViewBlock = click;
    [self noDataWithMessage:message sink:0 click:click];
}
/**
 *  没有数据的时候显示的图片
 *
 *  @pram sink 位置下沉的距离
 */
- (void)noDataWithSink:(CGFloat)sink click:(void (^)(void))click {
    BaseInfo.clickNoDataViewBlock = click;
    [self noDataWithMessage:nil sink:sink click:click];
}
/**
 *  没有数据的时候显示的图片
 *
 *  @pram message 显示的文字
 *  @pram sink 位置下沉的距离
 */
- (void)noDataWithMessage:(NSString *)message sink:(CGFloat)sink click:(void (^)(void))click {
    BaseInfo.clickNoDataViewBlock = click;
    UIView *noDataView = [self viewWithTag:TAG_NoDataImageViewTag];
    if (noDataView == nil) {
        noDataView = [[UIView alloc] init];
        [self addSubview:noDataView];
        noDataView.tag = TAG_NoDataImageViewTag;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGFloat width = CGRectGetWidth(self.bounds);
        CGFloat height = CGRectGetHeight(self.bounds) - sink;
        CGFloat value = width > height ? height : width;
        if (value > NoDataImagepHeight) {
            value = NoDataImagepHeight;
        }
        if (message.length > 0) {
            noDataView.frame = CGRectMake((width - value) * 0.5, (height - value - 30) * 0.5 + sink, value, value + 30);
        } else {
            noDataView.frame = CGRectMake((width - value) * 0.5, (height - value) * 0.5 + sink, value, value);
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:NoDataImageName]];
        imageView.frame = CGRectMake(0, 0, value, value);
        [noDataView addSubview:imageView];
        if (message.length > 0) {
            UILabel *label = [[UILabel alloc] init];
            [noDataView addSubview:label];
            label.text = message;
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(noDataView);
                make.top.equalTo(imageView.mas_bottom);
            }];
            label.font = [UIFont systemFontOfSize:CoreInfo.fontSize * 0.8];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = TYZLightColor;
        }
        UIButton *coverButton = [[UIButton alloc] initWithFrame:noDataView.bounds];
        [noDataView addSubview:coverButton];
        [coverButton addTarget:self action:@selector(didClickCoverButton) forControlEvents:UIControlEventTouchUpInside];
    });
}
- (void)didClickCoverButton {
    if (BaseInfo.clickNoDataViewBlock) {
        BaseInfo.clickNoDataViewBlock();
    }
}
- (void)removeNoDataImage {
    BaseInfo.clickNoDataViewBlock = nil;
    UIView *noDataView = [self viewWithTag:TAG_NoDataImageViewTag];
    [noDataView removeFromSuperview];
}
- (UIView *)addLine {
    return [self addLineWithHieght:0.5];
}
- (UIView *)addLineInBottom {
    return [self addLine];
}
- (UIView *)addLineInBottomWithHieght:(CGFloat)height {
    return [self addLineInBottomWithHieght:height];
}
- (UIView *)addLineWithHieght:(CGFloat)height {
    UIView *line = [[UIView alloc] init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(height);
    }];
    line.backgroundColor = TYZLineColor;
    return line;
}
- (UIView *)addLineInTop {
    return [self addLineInTopWithHieght:0.5];
}
- (UIView *)addLineInTopWithHieght:(CGFloat)height {
    UIView *line = [[UIView alloc] init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(height);
    }];
    line.backgroundColor = TYZLineColor;
    return line;
}
- (UIView *)addLineInRight {
    return [self addLineInRightWithEmptyFloat:0 width:0.5];
}
- (UIView *)addLineInRightWithEmptyFloat:(CGFloat)emptyFloat width:(CGFloat)width {
    UIView *line = [[UIView alloc] init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.top.equalTo(self).offset(emptyFloat);
        make.right.equalTo(self);
        make.width.mas_equalTo(width);
    }];
    line.backgroundColor = TYZLineColor;
    return line;
}
- (UIView *)addLineInLeft {
    return [self addLineInLeftWithEmptyFloat:0 width:0.5];
}
- (UIView *)addLineInLeftWithEmptyFloat:(CGFloat)emptyFloat width:(CGFloat)width {
    UIView *line = [[UIView alloc] init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.top.equalTo(self).offset(emptyFloat);
        make.left.equalTo(self);
        make.width.mas_equalTo(width);
    }];
    line.backgroundColor = TYZLineColor;
    return line;
}
- (void)cornerRadius:(CGFloat)value {
    self.layer.cornerRadius = value;
    self.layer.masksToBounds = YES;
}
- (void)borderColor:(UIColor *)color width:(CGFloat)width {
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
}
@end
